import { defineStore } from 'pinia'

type EffectFn<P> = (option: Partial<RequestOption<P>>) => void

export const createRequestStore = <R, P = any>(
  storeId: string,
  handler: EffectFn<P>
) => {
  return defineStore(storeId, () => {
    const state = ref<RequestReactive<P, R>>()
    return {
      state,
      fetch: (option?: Partial<RequestOption<P>>) => {
        state.value = handler(option || {}) as any
      }
    }
  })
}
